home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OrganicDesktopPaneUI.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  262 lines

  1. /*
  2.  * @(#)OrganicDesktopPaneUI.java    1.5 98/02/05
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.   
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.plaf.ComponentUI;
  25. import java.awt.event.*;
  26. import java.awt.Component;
  27. import java.awt.Point;
  28. import java.awt.Dimension;
  29. import java.awt.Insets;
  30. import java.awt.Graphics;
  31. import com.sun.java.swing.plaf.DesktopPaneUI;
  32. import com.sun.java.swing.plaf.basic.BasicDesktopPaneUI;
  33. import java.util.*;
  34.  
  35. /**
  36.  * This class is in charge of display of the OrganicDesktop.  It creates a OrganicDesktopMenu
  37.  * which it installs inside itself.  It then registers for MouseEvents.  When the
  38.  * mouse enters the OrganicDesktopMenu it pops down a menu to display the iconized windows.
  39.  * <p>
  40.  * Warning: serialized objects of this class will not be compatible with
  41.  * future swing releases.  The current serialization support is appropriate
  42.  * for short term storage or RMI between Swing1.0 applications.  It will
  43.  * not be possible to load serialized Swing1.0 objects with future releases
  44.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  45.  * baseline for the serialized form of Swing objects.
  46.  *
  47.  * @see OrganicDesktopManager
  48.  * @see OrganicDesktopMenu
  49.  * @version 1.5 02/05/98
  50.  * @author Steve Wilson
  51.  */
  52. public class OrganicDesktopPaneUI extends BasicDesktopPaneUI 
  53.                               implements MouseListener, ActionListener, ContainerListener {
  54.  
  55. //    OrganicDesktopManager organicDesktopManager;
  56.     OrganicDesktopMenu desktopMenu;
  57.     boolean desktopMenuRemoved = false;
  58.  
  59.     /**
  60.       * This fuction creates and returns a new OrganicDesktopPaneUI.
  61.       * in the current implementation each JDesktopPane needs its own UI.
  62.       * They cannot be shared.  BAD THINGS WOULD HAPPEN.
  63.       */
  64.     public static ComponentUI createUI(JComponent c) {
  65.         return new OrganicDesktopPaneUI();
  66.     }
  67.  
  68.     public OrganicDesktopPaneUI() {
  69.         super();
  70.     }
  71.  
  72.     /**
  73.       * This fuction creates the OrganicDesktopManager.
  74.       * It also causes all JDesktopIcons to be removed from the 
  75.       * container and put into the desktop menu.
  76.       * It also creates and installs the OrganicDesktopMenu.
  77.       *
  78.       */
  79.     public void installUI(JComponent c)   {
  80.         super.installUI(c);
  81.     installDesktopMenu(c);
  82.     installListeners(c);
  83.  
  84.     }
  85.  
  86.     /**
  87.       * This replaces desktop icons back to the Desktop.
  88.       * It also kills the DesktopMenu.
  89.       *
  90.       */    
  91.     public void uninstallUI(JComponent c)   {
  92.     uninstallListeners(c);
  93.         replaceDesktopIcons();
  94.     uninstallDesktopMenu(c);
  95.         super.uninstallUI(c);
  96.     }
  97.  
  98.     protected void installListeners(JComponent c) {
  99.         desktopMenu.addMouseListener(this);
  100.     desktop.addComponentListener(desktopMenu);
  101.     desktop.addContainerListener(this);
  102.     }
  103.  
  104.     protected void uninstallListeners(JComponent c) {
  105.     desktopMenu.removeMouseListener(this);
  106.     desktop.removeContainerListener(this);
  107.     desktop.removeComponentListener(desktopMenu);
  108.     }
  109.  
  110.     protected void installDesktopManager() {
  111.     if(desktop.getDesktopManager() == null) {
  112.         desktopManager = new OrganicDesktopManager();
  113.         desktop.setDesktopManager(desktopManager);
  114.         ((OrganicDesktopManager)desktopManager).setTheDesktop(desktop);
  115.         ((OrganicDesktopManager)desktopManager).setDesktopUI(this);
  116.     }
  117.     }
  118.  
  119.     protected void installDesktopMenu(JComponent c){
  120.         desktopMenu = new OrganicDesktopMenu(this);
  121.     desktop.add(desktopMenu);
  122.     desktopMenu.locateInParent(c);
  123.     removeDesktopIcons();
  124.     desktopMenu.change();
  125.     }
  126.  
  127.     protected void uninstallDesktopMenu(JComponent c) {
  128.     desktop.remove(desktopMenu);
  129.     }
  130.  
  131.  
  132.     public void paint(Graphics g, JComponent c) {
  133.         if (desktopMenuRemoved) {
  134.         desktop.add(desktopMenu);
  135.         desktopMenu.locateInParent(desktop);
  136.         desktopMenuRemoved = false;
  137.     }
  138.     }            
  139.     /**
  140.       * This function removes any DesktopIcons from the desktop
  141.       * and asks the DesktopManager to place them in the iconizedWindowList.
  142.       * It is called by installUI.
  143.       *
  144.       */   
  145.     protected void removeDesktopIcons() {
  146.          Component[] framesAndIcons = desktop.getComponents();
  147.          for (int i = 0; i < framesAndIcons.length; i++) {
  148.              if ( framesAndIcons[i] instanceof JInternalFrame.JDesktopIcon ) {
  149.                ((OrganicDesktopManager)desktopManager).addToIconizedWindowList( (JInternalFrame.JDesktopIcon)framesAndIcons[i] );
  150.                desktop.remove(framesAndIcons[i]);
  151.          } // end if
  152.        }  // end for i
  153.     }
  154.  
  155.     /**
  156.       * This function removes InternalFrames from the IconizedWindowList
  157.       * and places the DesktopIcons into the desktop.
  158.       * It is called by uninstallUI.
  159.       *
  160.       */   
  161.       protected void replaceDesktopIcons() {
  162.           Vector iconizedWindowList = ((OrganicDesktopManager)desktopManager).getIconizedWindows();
  163.       Enumeration iter = iconizedWindowList.elements();
  164.       while (iter.hasMoreElements()) {
  165.           JInternalFrame frame = (JInternalFrame)iter.nextElement();
  166.           JInternalFrame.JDesktopIcon icon = frame.getDesktopIcon();
  167.           ((OrganicDesktopManager)desktopManager).replaceDesktopIcon( icon );
  168.       } // end while
  169.     }
  170.  
  171.     /**
  172.       * This is an implementation of a MouseListener function.
  173.       * and asks the DesktopManager to place them in the iconizedWindowList.
  174.       * It is called by installUI.
  175.       *
  176.       */   
  177.     public void mouseEntered(MouseEvent e) {
  178.     showSelectionMenu();
  179.     }
  180.  
  181.      /** This function shows the popup menu which allows the user to select a window to deiconify.
  182.        * It is called by mouseEntered.
  183.        *
  184.        */      
  185.       protected void showSelectionMenu() {
  186.     Vector iconizedWindows = getIconizedWindows();
  187.     JPopupMenu menu = new JPopupMenu();
  188.     JMenuItem item = null;
  189.     Enumeration iter = iconizedWindows.elements();
  190.     int windowNumber = 0;
  191.     while (iter.hasMoreElements()) {  // put each iconized window into the PopupMenu
  192.         JInternalFrame window = (JInternalFrame)iter.nextElement();
  193.         item = new JMenuItem(window.getTitle(), window.getFrameIcon());
  194.         menu.add(item);
  195.         item.setActionCommand(String.valueOf(windowNumber));
  196.         item.addActionListener(this);
  197.         windowNumber++;
  198.     }
  199.     if (windowNumber != 0) {  // prevent showing empty menu.
  200.         java.awt.Point loc = desktopMenu.getPopupLocation(menu);
  201.         menu.show(desktopMenu, loc.x, loc.y);
  202.     }
  203.     }
  204.  
  205.     /** This function returns the list of all iconizedWindows.
  206.       */
  207.     public Vector getIconizedWindows() {
  208.         return ((OrganicDesktopManager)desktopManager).getIconizedWindows();
  209.     }
  210.  
  211.     /** This function is called each time a frame is iconified.
  212.       * it is called by OrganicDesktopManager.
  213.       * this notification is used to tell the DesktopMenu to update.
  214.       */
  215.     public void frameIconified() {
  216.         desktopMenu.change();
  217.     }
  218.  
  219.     /** This function is called each time a frame is deiconified.
  220.       * it is called by OrganicDesktopManager.
  221.       * this notification is used to tell the DesktopMenu to update.
  222.       */
  223.     public void frameDeiconified() {
  224.         desktopMenu.change();
  225.     }
  226.  
  227.     /** This is an implemention of ActionListener.
  228.       * this function is called when the user selects an item from the menu
  229.       * which is created in the showDesktopMenu
  230.       */
  231.     public void actionPerformed(ActionEvent e) {
  232.         String selectedWindowNumberAsString = e.getActionCommand();
  233.     int selectedWindowNumber = Integer.parseInt(selectedWindowNumberAsString);
  234.     JInternalFrame frameToDeiconify = 
  235.             (JInternalFrame)((OrganicDesktopManager)desktopManager).getIconizedWindows().elementAt(selectedWindowNumber);
  236.     ((OrganicDesktopManager)desktopManager).deiconifyFrame(frameToDeiconify);
  237.     ((JComponent)e.getSource()).getParent().setVisible(false);
  238.     }
  239.  
  240.     /*
  241.      * If someone removes the desktop menu.  Put it back in!
  242.      */
  243.     public void componentRemoved(ContainerEvent e) {
  244.         Component c = e.getChild();  // the removed component
  245.     if ( c == desktopMenu ) {
  246.         desktopMenuRemoved = true;
  247.         desktop.repaint();
  248.     }
  249.     }
  250.             
  251.     public void mouseExited(MouseEvent e) {}  // stubbed for MouseListener
  252.  
  253.     public void mousePressed(MouseEvent e) {}// stubbed for MouseListener
  254.  
  255.     public void mouseReleased(MouseEvent e) {}// stubbed for MouseListener
  256.  
  257.     public void mouseClicked(MouseEvent e) {}// stubbed for MouseListener
  258.  
  259.     public void componentAdded(ContainerEvent e) {}// stubbed for ContainerListener
  260.  
  261. }
  262.